home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_09 / barbu2 / linein.cpp < prev    next >
C/C++ Source or Header  |  1995-05-07  |  2KB  |  118 lines

  1. //////// AB CLASSGEN Mon May 01 19:18:11 1995 ////////
  2. // LINEIN Implementation
  3. //////////////////////////////////////////////////////
  4. #include "LINEIN.HPP"
  5. #include <stdio.h>
  6. #include <values.h>
  7. #include <ctype.h>
  8. #include <dir.h>
  9.  
  10. LINEIN::LINEIN(const char far lpszText[])
  11. {
  12. _commonCtorJob(lpszText);
  13. }
  14.  
  15. LINEIN::LINEIN(HINSTANCE hInstance,
  16.             const char szFileOrResName[],
  17.             const char szResType[])
  18. {
  19. char far *lpText;
  20. _s = 0;
  21. if(0 != szResType){    // load from resource
  22.     HGLOBAL hRes = LoadResource(hInstance,
  23.                         FindResource(hInstance,
  24.                             szFileOrResName,
  25.                             szResType
  26.                             )
  27.                         );
  28.     lpText = (char far*)LockResource(hRes);
  29.     _commonCtorJob(lpText);
  30.     GlobalUnlock(hRes);
  31.     FreeResource(hRes);
  32.     }
  33. else{                // load from file
  34.     struct ffblk ff;
  35.     if(0 != findfirst(szFileOrResName, &ff, 0))
  36.         return;
  37.     long size = ff.ff_fsize;
  38.     if(size >= MAXINT)
  39.         return;
  40.     if(0 == (lpText = new char[1+size]))
  41.         return;
  42.     lpText[size] = 0;
  43.     FILE* f = fopen(szFileOrResName, "rt");
  44.     fread(lpText, 1, size, f);
  45.     fclose(f);
  46.     _commonCtorJob(lpText);
  47.     delete lpText;
  48.     }
  49. }
  50.  
  51. void LINEIN::_commonCtorJob(const char far lp[])
  52. {
  53. _x = 0;
  54. if(0 == (_s = new STR(1+strlen(lp))))
  55.     return;
  56. int i = 0;
  57. while(isspace(lp[i]))
  58.     i++;
  59. while(lp[i]){
  60.     if(i > 0 && lp[i] == '\\' && lp[i-1] !='\\'
  61.             && lp[i+1] == '\n'){
  62.         do i++; while(isspace(lp[i]));
  63.         }
  64.     *_s += lp[i++];
  65.     if(lp[i-1] == '\n'){
  66.         while(isspace(lp[i]))
  67.             i++;
  68.         }
  69.     }
  70. }
  71.  
  72. LINEIN::~LINEIN()
  73. {
  74. if(0 != _s){
  75.     delete _s;
  76.     _s = 0;
  77.     }
  78. }
  79.  
  80. void LINEIN::reset()
  81. {
  82. _x = 0;
  83. }
  84.  
  85. static void Copy(const char* p, int i, int j, STR& dest)
  86. {
  87. STR temp(j-i+1);
  88. for(int k = i; k < j; k++)
  89.     temp += p[k];
  90. dest = temp;
  91. }
  92.  
  93. const char * LINEIN::get(STR& Line)
  94. {
  95. if(0 == _s || -1 == _x)
  96.     return 0;
  97. int i = _s->hasin('\n', _x);
  98. if(-1 == i){
  99.     _x = -1;
  100.     if(COMM != (*_s)[_x] && _x != _s->len()-1){
  101.         Copy(*_s, _x, _s->len()-1, Line);
  102.         return Line;
  103.         }
  104.     return 0;
  105.     }
  106. if(COMM != (*_s)[_x]){
  107.     Copy(*_s, _x, i, Line);
  108.     _x = i+1;
  109.     return Line;
  110.     }
  111. else{
  112.     _x = i+1;
  113.     return get(Line);
  114.     }
  115. }
  116.  
  117.  
  118.